﻿using System;
using System.Collections.Generic;
using System.Linq;
using FootballProject.DAL.DatatAccessLib;

namespace FootballProject.BL.BusinessManagers
{
    /// <summary>
    /// Author: Ivan Shikht
    /// </summary>
    public class RefereeManager
    {
        #region Fields
        private readonly EntitiesContext _context;
        #endregion

        #region Constructors
        public RefereeManager()
        {
            _context = new EntitiesContext();
        }
        #endregion

        #region Methods

        public List<Referee> FindAll()
        {
            IQueryable<Referee> rReferee = from referee in _context.RefereeSet
                                           select referee;
            return rReferee.ToList();
        }


        public List<Referee> FindRefereesByName(string refereeName)
        {
            IQueryable<Referee> rReferee = from referee in _context.RefereeSet
                                           where referee.RefereeName.StartsWith(refereeName)
                                           select referee;
            return rReferee.ToList();
        }

        public Referee FindRefereeById(Guid refereeId)
        {
            IQueryable<Referee> rReferee = from referee in _context.RefereeSet
                                           where referee.RefereeID == refereeId
                                           select referee;
            return rReferee.First();
        }

        public void UpdateReferee(Referee referee)
        {
            _context.Attach(referee);
            referee.SetAllModified(_context);
            _context.SaveChanges();
        }

        public void AddReferee(Referee referee)
        {
            _context.AddToRefereeSet(referee);
            _context.SaveChanges();
        }


        public void DeleteReferee(Referee referee)
        {
            _context.Attach(referee);
            _context.DeleteObject(referee);
            _context.SaveChanges();
        }
        #endregion
    }
}